import { forEach, startsWith } from 'lodash';
import { Row, Col, Input, Button, message, Table } from 'antd';

import styles from '../TableList.less';

function generateScoreMap(phaseCode, scoreList) {
  var scoreMap = {};
  scoreList.map(element => {
    if (phaseCode && phaseCode !== element.phaseCode) {
      return
    }
    const fenzu = element.fenzuName;
    const gamePos1 = element.gamePosName1;
    const gamePos2 = element.gamePosName2;
    if (fenzu !== '') {
      if (!scoreMap[fenzu]) {
        scoreMap[fenzu] = {};
        scoreMap[fenzu].columns = [];
        scoreMap[fenzu].data = [];
        scoreMap[fenzu].scores = {};
      }

      if (!listContains(scoreMap[fenzu].columns, gamePos1)) {
        scoreMap[fenzu].columns.push(gamePos1);
      }
      if (!listContains(scoreMap[fenzu].columns, gamePos2)) {
        scoreMap[fenzu].columns.push(gamePos2);
      }

      if (!scoreMap[fenzu].scores[gamePos1]) {
        scoreMap[fenzu].scores[gamePos1] = {};
        if(gamePos1){
          const fenzuName = gamePos1.substring(0,1)
          const fenzuSeq = gamePos1.substring(1)
          scoreMap[fenzu].scores[gamePos1].fenzuName = fenzuName
          scoreMap[fenzu].scores[gamePos1].fenzuSeq = fenzuSeq
        }
        scoreMap[fenzu].scores[gamePos1].gamePos = gamePos1;
        scoreMap[fenzu].scores[gamePos1].athlete = element.athlethList1;

      }

      if (!scoreMap[fenzu].scores[gamePos2]) {
        scoreMap[fenzu].scores[gamePos2] = {};
        if(gamePos2){
          const fenzuName = gamePos2.substring(0,1)
          const fenzuSeq = gamePos2.substring(1)
          scoreMap[fenzu].scores[gamePos2].fenzuName = fenzuName
          scoreMap[fenzu].scores[gamePos2].fenzuSeq = fenzuSeq
        }
        scoreMap[fenzu].scores[gamePos2].gamePos = gamePos2;
        scoreMap[fenzu].scores[gamePos2].athlete = element.athlethList2;

      }
    }
  }); 

  forEach(scoreMap, element => {
    forEach(element.scores, value => {
      value[value.gamePos] = {isnull:true}
      element.data.push(value);
    });
    element.data.sort(comparePos);
  });

  return scoreMap;
};

function listContains(arr, obj) {
  var i = arr.length;
  while (i--) {
    if (arr[i] === obj) {
      return true;
    }
  }
  return false;
}

const comparePos = (obj1, obj2) => {
  var val1 = obj1.fenzuName;
  var val2 = obj2.fenzuName;
  var val3 = parseInt(obj1.fenzuSeq);
  var val4 = parseInt(obj2.fenzuSeq);
  if (val1 < val2) {
    return -1;
  } else if (val1 > val2) {
    return 1;
  } else {
    if (val3 < val4) {
      return -1;
    } else if (val3 > val4) {
      return 1;
    } else {
      return 0;
    }
  }
};

export function generateChouqianTable(phaseCode, scoreList) {
  const scoreMap = generateScoreMap(phaseCode, scoreList);
  const tables = [];
  forEach(scoreMap, (value, key) => {
    tables.push(
      <Table
        key={key}
        pagination={false}
        bordered
        size="small"
        style={{ marginBottom: 30 }}
        dataSource={value.data}
        columns={generateScoreField(key, value.columns)}
      />
    );
  });
  return tables;
};

const compareColumn = (obj1, obj2) => {
  if(obj1 && obj2){
    var val1 = obj1.substring(0,1);
    var val2 = obj2.substring(0,1);
    var val3 = parseInt(obj1.substring(1));
    var val4 = parseInt(obj2.substring(1));
    if (val1 < val2) {
      return -1;
    } else if (val1 > val2) {
      return 1;
    } else {
      if (val3 < val4) {
        return -1;
      } else if (val3 > val4) {
        return 1;
      } else {
        return 0;
      }
    }
  }else if(obj1){
    return -1;
  }else if(obj2){
    return 1;
  }else{
    return 0;
  }
  
};

function generateScoreField(key, fieldList) {
  fieldList.sort(compareColumn)
  const fileds = [
    {
      title: <span style={{ fontSize: 18, color: 'red' }}>{key + '组'}</span>,
      dataIndex: 'gamePos',
      width: 100,
      align: 'center',
    },
    {
      title: '',
      dataIndex: 'athlete',
      width: 150,
      align: 'center',
      render: value => (
        <span>
          {value && value
            .map(value => {
              return value.userName;
            })
            .join(',')}
        </span>
      ),
    },
  ];
  fieldList.forEach(value => {
    fileds.push({
      title: value,
      width: 150,
      className:styles.columnNopadding,
      align: 'center',
      render: (text, obj, index) =>
        obj[value] && obj[value].isnull ? <div className={styles.out}>
        </div>:(
          <div></div>
        ),
    });
  });

  fileds.push({
    title: '',
    key:'sdfasd',
    align: 'center',
  });

  return fileds;
};